package com.hdcircles.autoform.ui.label.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hdcircles.autoform.R;
import com.hdcircles.autoform.model.bean.FormBean;
import com.hdcircles.autoform.util.SystemUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by hdcircles on 2016/10/18.
 */

public class FormListAdapter extends RecyclerView.Adapter<FormListAdapter.ViewHolder> {

    private List<FormBean> formBeanList;

    private onItemClickListener onItemClickListener;

    private getUserNameListener getUserNameListener;

    public FormListAdapter(List<FormBean> formBeanList){
        this.formBeanList=formBeanList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_form_list, parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        FormBean form=formBeanList.get(position);
        holder.tvTitle.setText(form.getFormName());
        String time=holder.itemView.getContext().getResources().getString(R.string.fill_time
                , SystemUtil.long2Date(form.getTime()));
        if (null!=form.getWriterName()){
//            getUserNameListener.getUserName(time,form.getReceiver(),holder.tvTime);
            holder.tvTime.setText(time+"\t\t"+form.getWriterName());
        }else {
//            String receiver=null==form.getReceiver()?"":form.getReceiver();

            holder.tvTime.setText(time);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClickListener.onItemClick(formBeanList.get(holder.getAdapterPosition()),view);
            }
        });
    }

    public void setOnItemClickListener(FormListAdapter.onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setGetUserNameListener(FormListAdapter.getUserNameListener getUserNameListener) {
        this.getUserNameListener = getUserNameListener;
    }

    public interface onItemClickListener{
        void onItemClick(FormBean form,View view);
    }

    public interface getUserNameListener{
        void getUserName(String time,String receiver,TextView textView);
    }

    @Override
    public int getItemCount() {
        return null == formBeanList ? 0 : formBeanList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_question_title)
        TextView tvTitle;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.iv_more)
        ImageView ivMore;

        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
